package com.lp.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lp.DefineException.DBHelperException;
import com.lp.entity.Project;
import com.lp.entity.ProjectMember;
import com.lp.utils.DBHelper;
import com.lp.utils.RowToObject;

public class ProjectDAO
{
	private DBHelper<Project> dbHelper;
	
	private final String sql_fix = "select * from project where id = ?";
	
	public ProjectDAO()
	{
		dbHelper = new DBHelper<Project>();
	}
	
	public Project findProjectById(int id)
	{
		return dbHelper.myExecuteQuerySingle(sql_fix, new RowToProject(), id);
	}
	
	public List<Project> findProjectById(int[] ids)
	{
		String[] sqls = new String[ids.length];
		Object[][] objects = new Object[sqls.length][1];
		
		for(int i = 0;i < ids.length;i++)
		{
			sqls[i] = sql_fix;
			
			objects[i][0] = ids[i];
		}
		
		return dbHelper.myExecuteMultiSqls(sqls, new RowToProject(), objects);
	}
	
	public List<Project> findProjectByPrincipalId(int project_princialId)
	{
		String sql = "select * from project where project_principalId = ?";
		return dbHelper.myExecuteQueryMulti(sql, new RowToProject(), project_princialId);
	}
	
	
	private class RowToProject implements RowToObject<Project>
	{

		public Project rowToObject(ResultSet rs)
		{
			Project project = new Project();
			try
			{
				project.setId(rs.getInt("id"));
				project.setProject_name(rs.getString("project_name"));
				project.setProject_description(rs.getString("project_description"));
				project.setProject_principalId(rs.getInt("project_principalId"));
				project.setProject_state(rs.getBoolean("project_state"));
			}
			catch (SQLException e)
			{
				throw new DBHelperException(e.getMessage(),e);
			}
			return null;
		}
		
	}

}
